home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / HttpURLConnection.java < prev    next >
Text File  |  1998-09-22  |  7KB  |  244 lines

  1. /*
  2.  * @(#)HttpURLConnection.java    1.10 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16. import java.io.IOException;
  17.  
  18. /**
  19.  * A URLConnection with support for HTTP-specific features. See
  20.  * <A HREF="http://www.w3.org/pub/WWW/Protocols/"> the spec </A> for
  21.  * details.  
  22.  * @since JDK1.1
  23.  */
  24. abstract public class HttpURLConnection extends URLConnection {
  25.     /* instance variables */
  26.  
  27.     /**
  28.      * @since   JDK1.1
  29.      */
  30.     protected String method = "GET";
  31.  
  32.     /**
  33.      * @since   JDK1.1
  34.      */
  35.     protected int responseCode = -1;
  36.  
  37.     /**
  38.      * @since   JDK1.1
  39.      */
  40.     protected String responseMessage = null;
  41.  
  42.  
  43.     /* static variables */
  44.  
  45.     /* do we automatically follow redirects? The default is true. */
  46.     private static boolean followRedirects = true;
  47.  
  48.     /* valid HTTP methods */
  49.     private static final String[] methods = {
  50.     "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE"
  51.     };
  52.  
  53.     /**
  54.      * Constructor for the URLStreamHandler.
  55.      * @since   JDK1.1
  56.      */
  57.     protected HttpURLConnection (URL u) {
  58.     super(u);
  59.     }
  60.     
  61.     /**
  62.      * Sets whether HTTP redirects  (requests with response code 3xx) should 
  63.      * be automatically followed by this class.  True by default.  Applets
  64.      * cannot change this variable.
  65.      * @since   JDK1.1
  66.      */
  67.     public static void setFollowRedirects(boolean set) {
  68.     SecurityManager sec = System.getSecurityManager();
  69.     if (sec != null) {
  70.         // seems to be the best check here...
  71.         sec.checkSetFactory();
  72.     }
  73.     followRedirects = set;
  74.     }
  75.  
  76.     /**
  77.      * @since   JDK1.1
  78.      */
  79.     public static boolean getFollowRedirects() {
  80.     return followRedirects;
  81.     }
  82.  
  83.     /**
  84.      * Set the method for the URL request, one of:
  85.      * <UL>
  86.      *  <LI>GET
  87.      *  <LI>POST
  88.      *  <LI>HEAD
  89.      *  <LI>OPTIONS
  90.      *  <LI>PUT
  91.      *  <LI>DELETE
  92.      *  <LI>TRACE
  93.      * </UL> are legal, subject to protocol restrictions.  The default
  94.      * method is GET.
  95.      * 
  96.      * @exception ProtocolException if the method cannot be reset or if
  97.      *              the requested method isn't valid for HTTP.
  98.      * @since     JDK1.1
  99.      */
  100.     public void setRequestMethod(String method) throws ProtocolException {
  101.     if (connected) {
  102.         throw new ProtocolException("Can't reset method: already connected");
  103.     }
  104.     // This restriction will prevent people from using this class to 
  105.     // experiment w/ new HTTP methods using java.  But it should 
  106.     // be placed for security - the request String could be
  107.     // arbitrarily long.
  108.  
  109.     for (int i = 0; i < methods.length; i++) {
  110.         if (methods[i].equals(method)) {
  111.         this.method = method;
  112.         return;
  113.         }
  114.     }
  115.     throw new ProtocolException("Invalid HTTP method: " + method);
  116.     }
  117.  
  118.     /**
  119.      * Get the request method.
  120.      * @since   JDK1.1
  121.      */
  122.     public String getRequestMethod() {
  123.     return method;
  124.     }
  125.     
  126.     /**
  127.      * Gets HTTP response status.  From responses like:
  128.      * <PRE>
  129.      * HTTP/1.0 200 OK
  130.      * HTTP/1.0 401 Unauthorized
  131.      * </PRE>
  132.      * Extracts the ints 200 and 401 respectively.
  133.      * Returns -1 if none can be discerned
  134.      * from the response (i.e., the response is not valid HTTP).
  135.      * @throws IOException if an error occurred connecting to the server.
  136.      * @since   JDK1.1
  137.      */
  138.     public int getResponseCode() throws IOException {
  139.     if (responseCode != -1) {
  140.         return responseCode;
  141.     }
  142.     // make sure we've gotten the headers
  143.     getInputStream();
  144.  
  145.     String resp = getHeaderField(0);
  146.     /* should have no leading/trailing LWS
  147.      * expedite the typical case by assuming it has
  148.      * form "HTTP/1.x <WS> 2XX <mumble>"
  149.      */
  150.     int ind;
  151.     try {    
  152.         ind = resp.indexOf(' ');
  153.         while(resp.charAt(ind) == ' ')
  154.         ind++;
  155.         responseCode = Integer.parseInt(resp.substring(ind, ind + 3));
  156.         responseMessage = resp.substring(ind + 4).trim();
  157.         return responseCode;
  158.     } catch (Exception e) { 
  159.         return responseCode;
  160.     }
  161.     }
  162.  
  163.     /**
  164.      * Gets the HTTP response message, if any, returned along with the
  165.      * response code from a server.  From responses like:
  166.      * <PRE>
  167.      * HTTP/1.0 200 OK
  168.      * HTTP/1.0 404 Not Found
  169.      * </PRE>
  170.      * Extracts the Strings "OK" and "Not Found" respectively.
  171.      * Returns null if none could be discerned from the responses 
  172.      * (the result was not valid HTTP).
  173.      * @throws IOException if an error occurred connecting to the server.
  174.      * @since  JDK1.1
  175.      */
  176.     public String getResponseMessage() throws IOException {
  177.     getResponseCode();
  178.     return responseMessage;
  179.     }
  180.  
  181.     /**
  182.      * Close the connection to the server.
  183.      * @since JDK1.1
  184.      */
  185.     public abstract void disconnect();
  186.  
  187.     /**
  188.      * Indicates if the connection is going through a proxy.
  189.      * @since   JDK1.1
  190.      */
  191.     public abstract boolean usingProxy();
  192.  
  193.     /**
  194.      * The response codes for HTTP, as of version 1.1.
  195.      */
  196.  
  197.     // REMIND: do we want all these??
  198.     // Others not here that we do want??
  199.  
  200.     /** 2XX: generally "OK" */
  201.     public static final int HTTP_OK = 200;
  202.     public static final int HTTP_CREATED = 201;
  203.     public static final int HTTP_ACCEPTED = 202;
  204.     public static final int HTTP_NOT_AUTHORITATIVE = 203; 
  205.     public static final int HTTP_NO_CONTENT = 204;
  206.     public static final int HTTP_RESET = 205;
  207.     public static final int HTTP_PARTIAL = 206;
  208.  
  209.     /** 3XX: relocation/redirect */
  210.     public static final int HTTP_MULT_CHOICE = 300;
  211.     public static final int HTTP_MOVED_PERM = 301;
  212.     public static final int HTTP_MOVED_TEMP = 302;
  213.     public static final int HTTP_SEE_OTHER = 303;
  214.     public static final int HTTP_NOT_MODIFIED = 304;
  215.     public static final int HTTP_USE_PROXY = 305;
  216.  
  217.     /** 4XX: client error */
  218.     public static final int HTTP_BAD_REQUEST = 400;
  219.     public static final int HTTP_UNAUTHORIZED = 401;
  220.     public static final int HTTP_PAYMENT_REQUIRED = 402;
  221.     public static final int HTTP_FORBIDDEN = 403;
  222.     public static final int HTTP_NOT_FOUND = 404;
  223.     public static final int HTTP_BAD_METHOD = 405;
  224.     public static final int HTTP_NOT_ACCEPTABLE = 406;
  225.     public static final int HTTP_PROXY_AUTH = 407;
  226.     public static final int HTTP_CLIENT_TIMEOUT = 408;
  227.     public static final int HTTP_CONFLICT = 409;
  228.     public static final int HTTP_GONE = 410;
  229.     public static final int HTTP_LENGTH_REQUIRED = 411;
  230.     public static final int HTTP_PRECON_FAILED = 412;
  231.     public static final int HTTP_ENTITY_TOO_LARGE = 413;
  232.     public static final int HTTP_REQ_TOO_LONG = 414;
  233.     public static final int HTTP_UNSUPPORTED_TYPE = 415;
  234.     
  235.     /** 5XX: server error */
  236.     public static final int HTTP_SERVER_ERROR = 500;
  237.     public static final int HTTP_INTERNAL_ERROR = 501;
  238.     public static final int HTTP_BAD_GATEWAY = 502;
  239.     public static final int HTTP_UNAVAILABLE = 503;
  240.     public static final int HTTP_GATEWAY_TIMEOUT = 504;
  241.     public static final int HTTP_VERSION = 505;
  242.  
  243. }
  244.